package com.ruoyi.neo.service.impl;

import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.neo.domain.PartView;
import com.ruoyi.neo.mapper.PartViewMapper;
import com.ruoyi.neo.repository.PartViewRepository;
import com.ruoyi.neo.service.IPartViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * 零件三视图Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-16
 */
@Service
public class PartViewServiceImpl implements IPartViewService {
    @Autowired
    private PartViewMapper partViewMapper;
    @Resource
    private PartViewRepository partViewRepository;

    /**
     * 查询零件三视图
     *
     * @param viewId 零件三视图主键
     * @return 零件三视图
     */
    @Override
    public PartView selectPartViewByViewId(Long viewId) {

        return partViewMapper.selectPartViewByViewId(viewId);
    }

    /**
     * 查询零件三视图列表
     *
     * @param partView 零件三视图
     * @return 零件三视图
     */
    @Override
    public List<PartView> selectPartViewList(PartView partView) {

        return partViewRepository.findViewList(partView.getPartNumber(), partView.getViewType(), partView.getViewImageUrl(), partView.getDescription());
    }

    @Override
    public List<String> selectAllPartNumber() {
        return partViewRepository.findAllPartNumber();
    }

    /**
     * 新增零件三视图
     *
     * @param partView 零件三视图
     * @return 结果
     */
    @Override
    public int insertPartView(PartView partView) {
        String viewNumber = partView.getPartNumber().toString()+partView.getViewType().toString();
        System.out.println(viewNumber);
        partView.setViewNumber(viewNumber);
        System.out.println(partView.getPartNumber());
        System.out.println(partView.getViewType());
        System.out.println(partView.getViewImageUrl());
        System.out.println(partView.getDescription());
        String viewNumberR = partViewRepository.addPartView(partView.getViewNumber(), partView.getPartNumber(), partView.getViewType(), partView.getViewImageUrl(), partView.getDescription());
        return viewNumberR == null ? 0 : 1;
    }

    /**
     * 修改零件三视图
     *
     * @param partView 零件三视图
     * @return 结果
     */
    @Override
    public int updatePartView(PartView partView) {

        return partViewRepository.updatePartView(partView.getViewNumber(), partView.getViewImageUrl(), partView.getDescription());
    }

    /**
     * 批量删除零件三视图
     *
     * @param viewIds 需要删除的零件三视图主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletePartViewByViewIds(Long[] viewIds) {
        List<Long> viewList = Arrays.asList(viewIds);
        try {
            int count = partViewRepository.deleteByViewIds(viewList);
            return count;
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletePartViewByViewNumber(String viewNumber) {
        try {
            int count = partViewRepository.delete(viewNumber);
            return count;
        } catch (Exception e) {
            return 0;
        }
    }
}
